package com.dongdongsongcan.app.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.dongdongsongcan.app.R;
import com.dongdongsongcan.app.adapter.AreaAdapter;
import com.dongdongsongcan.app.control.AddressControl;
import com.dongdongsongcan.app.control.SearchControl;
import com.dongdongsongcan.app.control.UserControl;
import com.dongdongsongcan.app.control.bean.AddressData;
import com.dongdongsongcan.app.control.bean.AreaData;
import com.su.dolphin.control.bean.BaseBean;
import com.su.dolphin.control.task.CtrlTask;
import com.su.dolphin.utils.mgr.DpActivityManager;
import com.su.dolphin.utils.ui.TipsUtil;
import com.su.dolphin.view.activity.BaseActivity;

import java.util.List;

public class SearchAddressActivity extends BaseActivity {
    Button mSearch_btn;
    ListView mSearch_list;
    EditText mSearch_et;
    AreaAdapter mAreaAdapter;

    public static void startFrom(Context context) {
        DpActivityManager.i().startActivity(context, SearchAddressActivity.class);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_search);
        setTitle("搜索地址");
    }

    @Override
    public void initView() {
        mSearch_et = (EditText) findViewById(R.id.search_et);
        mSearch_list = (ListView) findViewById(R.id.search_list);
        mAreaAdapter = new AreaAdapter(this);
        mSearch_list.setAdapter(mAreaAdapter);
    }

    @Override
    public void setListener() {
        mSearch_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TipsUtil.toast(mAreaAdapter.getData(position).area_name);
                AreaData.Area area = mAreaAdapter.getData(position);
                AddressData.Address address = new AddressData.Address();
                address.setInfo_louhao1(area.getArea_name());
                address.setInfo_name(UserControl.i().getUser().getUser_name());
                address.setInfo_mobile("15666220075");
                address.setInfo_sex("先生");
                address.setInfo_area("1");
                AddressControl.i().user_address_insert(address, new CtrlTask.IControlResponse<BaseBean>() {
                    @Override
                    public void onControlResponse(BaseBean data) {
                        TipsUtil.toast(data.getMsg());
                    }
                });
            }
        });
        mSearch_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String keyWord = s.toString();
                if (TextUtils.isEmpty(keyWord)) return;
                SearchControl.search(keyWord, new CtrlTask.IControlResponse<AreaData>() {
                    @Override
                    public void onControlResponse(AreaData data) {
                        if (data.isOk()) {
                            List<AreaData.Area> areas = data.getResult();
                            mAreaAdapter.setData(areas);
                        }
                    }
                });
            }
        });
    }

    @Override
    public void initData() {

    }

}
